/*
--------------------------------------------------------------------------------
				Ak-Lib. 
			 Project       	: Ak-Lib
			 Group         	: Ak-Lib R&D Team
			 Author        	: Ocean.Wang
			 Create Date 	: 05-01-2010
--------------------------------------------------------------------------------
Code Name 	:  A.K 	~v~
Description	:  Yet Another Cross-Platform Lib.
Cautions    	:
--------------------------------------------------------------------------------
Change History:
Version		: 0.1
Building 		: 05/01/10
10-05.01 first draft, build, debug and test.
--------------------------------------------------------------------------------
*/

#ifndef _INC_AK_APPL_
#define	_INC_AK_APPL_

#include "ak_types.h"
#include "ak_socket.h"
#include "ak_cli.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _t_appModReg{
	unsigned int 	modId;		/* User Custom the Module Id */
	char 		* 	modStr;		/* Use for Str Cpy */
	
	/* 1. Module Load & Init */
	_t_cbentry 		cbCtor;
	_t_cbentry 		cbDtor;

	/* 2. Module Status 
	**	The Module should return the Internal State as OOS/INS
	*/
	_t_cbentry 		cbProbe;
	
	/* 3. HeartBeat 
	** 	Periodicly Check the Module. Such as Thread, Queue, Memory, etc.
	*/
	_t_cbentry 		cbHeatBeat;

	/* 4. Fault Tolerant 
	**	It's the Module's Execption HOOK. Such as SegFault, Module should do some Log/Clean Job.
	*/
	_t_cbentry 		cbFault;

	/* 5. Command Register Interface for CLI 
	** 	It's the Module's CLI, supply a command line like interface to User.
	**	User Should Register all the Command in the Module Init.
	**	pModReg->cbCmdReg.cbProc( pModReg->cbCmdReg.pArg,
	**							 pModReg->cbCmdReg.lArg, 
	**							 pModule);
	**	
	*/
	_t_cbentry 	cbCmdReg;

}_t_appModReg;


extern	int 	SAgtApp_Init(char * appName);
extern	int 	SAgtApp_Fini(void);
extern	int 	SAgtApp_Exec(_t_cbentry * pCbPost);

extern	int 	SAgtApp_ModRegCmd(void * pModule, _t_cliCmdReg * pCmdReg);
extern	int 	SAgtApp_ModReg(_t_appModReg * pReg);
extern	int 	SAgtApp_ModRegA(_t_appModReg ** ppReg, int count);

#define _S_AGT_SOCKUN_INIT(pAddrUx, summix, iter) \
	do{\
		iter = SYS_SNPRINTF((pAddrUx)->sun_path, 	_SCAST_USOCK_NAME-1, _SCAST_USOCK_PREFIX, (summix)); \
		(pAddrUx)->sun_path[iter] = '\0'; 	\
		(pAddrUx)->sun_family = AF_UNIX;	\
	}while(0)

extern	int 	SAgtSock_InitUnix(int * pSock, S_SOCKET_ADDRUX * pAddrUx, char * summix);
extern	int 	SAgtSock_InitUdp(int * pSock, S_SOCKET_ADDRIN * pAddrIn, short port);


/* =====================================================================
** 	Application's Module report It's service, cpu, mem, thread to the FCast seperatly. _t_fcast_module.
**	FCast Collected  them into a _t_fcast_appagent. 
**	FCast BroadCast the _t_fcast_device & _t_fcast_appagent on the Network Periodicly.
**	1. Device Up
**	When a Device is Start. FCast Collect the _t_fcast_device 
**
**	2. Application UP
**	Application is started in order, controlled by FCast. 
**	After the last module is start successfully & Register to FCast, FCast Collect the _t_fcast_module.
**
**	3. BroadCast.
**	FCast BroadCast on the network of _t_fcast_device + _t_fcast_appagent.
**	It's Periodicly.
**
**	4. Application Update.
**	Including the Application is Modify, such as Administrator manualy start a new service, Or a Module
**	meets a Fault & Restart/Down.
**	FCast Collect the information and Update to Broadcast to Network Immediately.
**	The after Periodicly Broadcast should have already including the Application Update.
**	We Have the Applcation Update OP:
**	Service Add.
**	Service Del.
**	Service Modify.
**	Each Receiver should update it's own Local Service DB.
**
**	5. Device Update.
**	Including the Device Modify, such as Hot-Plug, & Part of the Device Down information. such as Memory
**	Fault, Hdisk Fault, etc.
**	FCast Collect the information and Update to Broadcast to Network Immediately.
**	The after Periodicly Broadcast should have already including the Application Update.
**	We Have the Device Update OP:
**	Device Add.
**	Device Del.
**	Device Modify.
**	Each Receiver should update it's own Local Device DB.
**	
**	6. Application / Device Down.
**	When the whole Application /Device Down, Such as Shutdown & Reset. We don't have a reliable method to
**	Notify others on the Network.
**	Such things is done by Timely Check on each Device. Using the LRU arithmetic.
**	That is. If a FCast Client couldn't receive a Periodicly Broadcast > 2 * Period, Client should disable the Entry
**	Of that Device, and think it's Pending.
**	Then a Probe should be done. (FCast <----> FCast). If Probe is Success or During that a new broadcast is
**	Received. Thinking the Entry of that Device is Active.
**	If Probe is failed. (Cost anthor Period of Broadcast, total 3*Period), Client should mark the Entry of that Device
** 	As OOS(out of service). And should Notify to the Local Application.
**	
**
** 	We should do some test on the NetLink Socket to InterComm with the Kernel.
**
*/
/* =====================================================================
**  Cast Control Message Interface
*/
#define _SCAST_PORT				5007
#define _SCAST_USOCK_PREFIX 	"/var/_cast_usock.%s"
#define _SCAST_USOCK_NAME 		0X40

#define _SCAST_MSG_MAXCNT 		0X100
#define _SCAST_MSG_MAXLEN 		0X200

#define _SCAST_APP_REG_REQ 		0X0001
#define _SCAST_APP_REG_RSP		0X0002
#define _SCAST_APP_UNR_REQ 		0X0003
#define _SCAST_APP_UNR_RSP		0X0004

typedef struct{
	unsigned short 	type;
	unsigned short 	length;
	unsigned char 	data[0];
}_t_castMsgHdr;


#define FCAST_MSGQ_TRY 		2

#ifdef __cplusplus
}
#endif

#endif


